package cn.sfrog.project.biz.admin.service.impl;

import cn.sfrog.common.core.domain.entity.SysUser;
import cn.sfrog.common.utils.SecurityUtils;
import cn.sfrog.project.biz.admin.domain.BizStudyProjectSponsor;
import cn.sfrog.project.biz.admin.domain.vo.BizStudyProjectSponsorFormVO;
import cn.sfrog.project.biz.admin.domain.vo.BizStudyProjectSponsorQueryVO;
import cn.sfrog.project.biz.admin.domain.vo.BizStudyProjectSponsorVO;
import cn.sfrog.project.biz.admin.mapper.BizStudyProjectSponsorMapper;
import cn.sfrog.project.biz.admin.service.IBizStudyProjectSponsorService;
import cn.sfrog.project.biz.web.vo.study.AppProjectDonateFormVO;
import cn.sfrog.system.domain.vo.BizStatusChangeVO;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


/**
 * 学研项目赞助Service业务层处理
 */
@Service
public class BizStudyProjectSponsorServiceImpl extends ServiceImpl<BizStudyProjectSponsorMapper, BizStudyProjectSponsor> implements IBizStudyProjectSponsorService {
    @Resource
    private BizStudyProjectSponsorMapper bizStudyProjectSponsorMapper;

    /**
     * 查询学研项目赞助
     *
     * @param id 学研项目赞助主键
     * @return 学研项目赞助
     */
    @Override
    public BizStudyProjectSponsorVO selectBizStudyProjectSponsorById(Long id) {
        return bizStudyProjectSponsorMapper.selectBizStudyProjectSponsorById(id);
    }

    /**
     * 查询学研项目赞助列表
     *
     * @param query 学研项目赞助
     * @return 学研项目赞助
     */
    @Override
    public List<BizStudyProjectSponsorVO> selectBizStudyProjectSponsorList(BizStudyProjectSponsorQueryVO query) {
        return bizStudyProjectSponsorMapper.selectBizStudyProjectSponsorList(query);
    }

    /**
     * 新增学研项目赞助
     *
     * @param form 学研项目赞助
     * @return 结果
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public boolean insertBizStudyProjectSponsor(BizStudyProjectSponsorFormVO form) {
        BizStudyProjectSponsor bizStudyProjectSponsor = new BizStudyProjectSponsor();
        BeanUtils.copyProperties(form, bizStudyProjectSponsor);
        bizStudyProjectSponsor.setCreateTime(new Date());
        bizStudyProjectSponsor.setCreateBy(SecurityUtils.getUsername());
        bizStudyProjectSponsor.setUpdateTime(new Date());
        bizStudyProjectSponsor.setUpdateBy(SecurityUtils.getUsername());
        return bizStudyProjectSponsor.insert();
    }

    /**
     * 修改学研项目赞助
     *
     * @param form 学研项目赞助
     * @return 结果
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public boolean updateBizStudyProjectSponsor(BizStudyProjectSponsorFormVO form) {
        BizStudyProjectSponsor bizStudyProjectSponsor = new BizStudyProjectSponsor();
        BeanUtils.copyProperties(form, bizStudyProjectSponsor);
        bizStudyProjectSponsor.setUpdateTime(new Date());
        bizStudyProjectSponsor.setUpdateBy(SecurityUtils.getUsername());
        return bizStudyProjectSponsor.updateById();
    }

    /**
     * 批量删除学研项目赞助
     *
     * @param ids 需要删除的学研项目赞助主键
     * @return 结果
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public boolean deleteBizStudyProjectSponsorByIds(Long[] ids) {
            return BizStudyProjectSponsor.DAO.delete(Wrappers.lambdaQuery(BizStudyProjectSponsor.class).in(BizStudyProjectSponsor::getId, (Object[]) ids));
    }

    @Transactional(rollbackFor = Throwable.class)
    @Override
    public boolean changeStatus(BizStatusChangeVO vo) {
//        return BizStudyProjectSponsor.DAO.update(Wrappers.lambdaUpdate(BizStudyProjectSponsor.class).in(BizStudyProjectSponsor::getId, (Object[]) vo.getIds()).set(BizStudyProjectSponsor::getStatus, vo.getStatus()));
        return true;
    }

    @Override
    public BizStudyProjectSponsor createStudyProjectSponsor(AppProjectDonateFormVO formVO, SysUser user,String serialNumber) {
        BizStudyProjectSponsor sponsor = new BizStudyProjectSponsor();
        sponsor.setProjectId(formVO.getProjectId());
        sponsor.setUserId(user.getUserId());
        sponsor.setSponsorAmount(formVO.getDonateAmount());
        sponsor.setSerialNumber(serialNumber);
        sponsor.setRemark(formVO.getRemark());
        sponsor.setUserName(user.getNickName());
        sponsor.setPhone(user.getPhonenumber());
        return sponsor;
    }
}
